package com.hsmw.schedule.task;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.schedule.mapper.HsmwVehicleMapper;
import com.hsmw.schedule.service.HsmwForceRemindService;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.util.SendSmsUtil;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.HsmwForceRemind;
import com.hsmw.domain.HsmwVehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Component
@EnableScheduling
@Configuration
@Slf4j
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class RemindScheduledTask {


    @Autowired
    private HsmwForceRemindService hsmwForceRemindService;

    @Autowired
    private HsmwVehicleMapper hsmwVehicleMapper;

    @Value(value = "${sms.appKey}")
    private String appKey;
    @Value(value = "${sms.channel}")
    private String channel;
    @Value(value = "${sms.smsSign}")
    private String smsSign;
    @Value(value = "${sms.appId}")
    private int appId;
    @Value(value = "${sms.remindForce}")
    private int remindForce;

    //@Scheduled(cron = "0 */1 * * * ?")
    private void DriverLeave() {

        log.info("查询提醒表定时任务开始时间：" + new DateTime().toString("yyyy-MM-dd HH:mm"));
        List<HsmwForceRemind> list = hsmwForceRemindService.list();
        list.stream().forEach(hsmwForceRemind -> {

            String vehicleModel = hsmwForceRemind.getVehicleModel();
            String vehicleType = hsmwForceRemind.getVehicleType();
            if(StringUtils.isNotBlank(vehicleModel) && StringUtils.isNotBlank(vehicleType)){
                List<HsmwVehicle> hsmwVehicles = hsmwVehicleMapper.selectList(new LambdaQueryWrapper<HsmwVehicle>().eq(HsmwVehicle::getVehicleModel, vehicleModel).eq(HsmwVehicle::getVehicleType, vehicleType).eq(HsmwVehicle::getWorkStatus, CommonConstants.VEHICLE_FREE));
                if(CollectionUtil.isNotEmpty(hsmwVehicles)){
                    String msg = "您需要的"+hsmwForceRemind.getVehicleType()+""+hsmwForceRemind.getVehicleModel()+"有空闲车辆了，您可以前往APP下单";
                    SendSmsUtil.sendDriverSmsCode(appId, appKey, msg,remindForce, smsSign, hsmwForceRemind.getUserPhone());
                    hsmwForceRemindService.removeById(hsmwForceRemind.getId());
                }
            }

        });

        log.info("查询提醒表定时任务结束时间：------>" + new DateTime().toString("yyyy-MM-dd HH:mm"));

    }


}
